package com.kxmall.admin.controller.storage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kxmall.admin.controller.storage.service.IKxStorageGoodsService;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.product.domain.KxStoreCategory;
import com.kxmall.product.mapper.KxStoreCategoryMapper;
import com.kxmall.storage.domain.KxStorageGoods;
import com.kxmall.storage.domain.bo.KxStorageGoodsBo;
import com.kxmall.storage.domain.bo.WarningStorageBo;
import com.kxmall.storage.domain.vo.KxStorageGoodsVo;
import com.kxmall.storage.mapper.KxStorageGoodsMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 仓库商品Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class KxStorageGoodsServiceImpl implements IKxStorageGoodsService {

    @Autowired
    private KxStoreCategoryMapper storeCategoryMapper;

    @Autowired
    private KxStorageGoodsMapper storageGoodsMapper;
    /**
     * 查询仓库商品
     */
    @Override
    public KxStorageGoodsVo queryById(Long id) {
        return storageGoodsMapper.selectVoById(id);
    }

    /**
     * 查询仓库商品列表
     */
    @Override
    public TableDataInfo<KxStorageGoodsVo> queryPageList(KxStorageGoodsBo bo, PageQuery pageQuery) {
        //LambdaQueryWrapper<KxStorageGoods> lqw = buildQueryWrapper(bo);
        Integer offset = (pageQuery.getPageNum() - 1) * pageQuery.getPageSize();
        Integer size = pageQuery.getPageSize();
        List<KxStorageGoodsVo> result = storageGoodsMapper. selectVoBySQL(offset,size,bo.getStorageId(),bo.getCategoryId()
                ,bo.getKeyword(),bo.getStatus(),bo.getNotIds());
        Long count = storageGoodsMapper.selectVoBySQLCount(bo.getStorageId(),bo.getCategoryId()
                ,bo.getKeyword(),bo.getStatus(),bo.getNotIds());
        return new TableDataInfo<KxStorageGoodsVo>(result,count);
    }

    /**
     * 查询仓库商品列表
     */
    @Override
    public List<KxStorageGoodsVo> queryList(KxStorageGoodsBo bo) {
        LambdaQueryWrapper<KxStorageGoods> lqw = buildQueryWrapper(bo);
        return storageGoodsMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KxStorageGoods> buildQueryWrapper(KxStorageGoodsBo bo) {
        LambdaQueryWrapper<KxStorageGoods> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStorageId() != null, KxStorageGoods::getStorageId, bo.getStorageId());
        lqw.eq(bo.getProductId() != null, KxStorageGoods::getProductId, bo.getProductId());
        lqw.eq(bo.getProductAttrId() != null, KxStorageGoods::getProductAttrId, bo.getProductAttrId());
        lqw.eq(bo.getCategoryId() != null, KxStorageGoods::getCategoryId, bo.getCategoryId());
        lqw.eq(bo.getStatus() != null, KxStorageGoods::getStatus, bo.getStatus());
        lqw.eq(bo.getStock() != null, KxStorageGoods::getStock, bo.getStock());
        lqw.eq(bo.getSales() != null, KxStorageGoods::getSales, bo.getSales());
        lqw.eq(bo.getFrezzStock() != null, KxStorageGoods::getFrezzStock, bo.getFrezzStock());
        lqw.eq(bo.getPrice() != null, KxStorageGoods::getPrice, bo.getPrice());
        lqw.eq(bo.getWarningNum() != null, KxStorageGoods::getWarningNum, bo.getWarningNum());
        return lqw;
    }

    /**
     * 新增仓库商品
     */
    @Override
    public Boolean insertByBo(KxStorageGoodsBo bo) {
        KxStorageGoods add = BeanUtil.toBean(bo, KxStorageGoods.class);
        add.setId(0L);
        validEntityBeforeSave(add);
        boolean flag = storageGoodsMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改仓库商品
     */
    @Override
    public Boolean updateByBo(KxStorageGoodsBo bo) {
        KxStorageGoods update = BeanUtil.toBean(bo, KxStorageGoods.class);
        validEntityBeforeSave(update);
        return storageGoodsMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KxStorageGoods entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除仓库商品
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return storageGoodsMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean freezeOrActivation(KxStorageGoodsBo bo) {
        KxStorageGoods add = new KxStorageGoods();
        add.setStatus(bo.getStatus());
        add.setUpdateTime(new Date());
        add.setId(bo.getId());
        return storageGoodsMapper.update(add, new LambdaQueryWrapper<KxStorageGoods>().eq(KxStorageGoods::getId, bo.getId())) > 0;
    }

    @Override
    public Boolean updateByStorageGoods(KxStorageGoodsBo bo) {
        return null;
    }

    @Override
    public Boolean updatePrice(KxStorageGoodsBo bo) {
        KxStorageGoods update = new KxStorageGoods();
        update.setUpdateTime(new Date());
        update.setPrice(bo.getPrice());
        update.setId(bo.getId());
        return storageGoodsMapper.update(update, new LambdaQueryWrapper<KxStorageGoods>().eq(KxStorageGoods::getId, bo.getId())) > 0;
    }

    @Override
    public TableDataInfo<KxStorageGoodsVo> queryPageWarningList(WarningStorageBo bo, PageQuery pageQuery) {
        //类目查询
        LinkedList<Long> childrenIds = new LinkedList<>();
        if (bo.getCategoryId() != null && bo.getCategoryId() != 0L) {
            List<KxStoreCategory> childrenList = storeCategoryMapper.selectList(new LambdaQueryWrapper<KxStoreCategory>().eq(KxStoreCategory::getPid, bo.getCategoryId()));
            if (!CollectionUtils.isEmpty(childrenList)) {
                //一级分类
                childrenList.forEach(item -> childrenIds.add(item.getId()));
                //使用in查询，就不需要等于查询
                bo.setCategoryId(null);
            }
        }
        Integer offset = (pageQuery.getPageNum() - 1) * pageQuery.getPageSize();
        Integer size = pageQuery.getPageSize();
        //查询制定页数的库存预警
        List<KxStorageGoodsVo> result = storageGoodsMapper.warningListByStoragePage(offset,size, bo);
        Long count = storageGoodsMapper.warningListByStoragePageCount(bo);
        return new TableDataInfo<KxStorageGoodsVo>(result,count);
    }

    @Override
    public Boolean warningUpdate(WarningStorageBo bo) {
        KxStorageGoods update = new KxStorageGoods();
        update.setUpdateTime(new Date());
        update.setWarningNum(bo.getNum());
        return storageGoodsMapper.update(update, new LambdaQueryWrapper<KxStorageGoods>()
                .eq(KxStorageGoods::getStorageId, bo.getStorageId())
                .eq(KxStorageGoods::getProductId,bo.getProductId())
                .eq(KxStorageGoods::getProductAttrId,bo.getProductAttrId())) > 0;
    }


}
